package com.cy.ppj.service.customer.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.JsonObjectMapper;
import com.cy.ppj.dao.mapper.customer.CustomerContactMapper;
import com.cy.ppj.dao.mapper.customer.CustomerImageMapper;
import com.cy.ppj.dao.mapper.customer.CustomerMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.ao.customer.CustomerAdminEditAO;
import com.cy.ppj.model.domian.customer.Customer;
import com.cy.ppj.model.domian.customer.CustomerContact;
import com.cy.ppj.model.enums.customer.CustomerImageType;
import com.cy.ppj.model.query.customer.CustomerListAdminQuery;
import com.cy.ppj.model.vo.customer.CustomerContactInfoVO;
import com.cy.ppj.model.vo.customer.CustomerDetailAdminVO;
import com.cy.ppj.model.vo.customer.CustomerImageListVO;
import com.cy.ppj.model.vo.customer.CustomerListAdminVO;
import com.cy.ppj.model.vo.customer.MallCustomerDetailAdminVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.customer.CustomerService;

/**
 * 客户service
 *
 * @author tanyilin
 *
 * @create 2019年2月18日 下午6:04:18
 */
@Service
public class CustomerServiceImpl extends BaseService implements CustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private CustomerContactMapper customerContactMapper;
    @Autowired
    private FileService fileService;
    @Autowired
    private CustomerImageMapper customerImageMapper;
	
	@Override
	public PageBean<CustomerListAdminVO> selectCustomerListAdmin(CustomerListAdminQuery query) {
		int total = customerMapper.selectCustomerListAdminCount(query);
		List<CustomerListAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = customerMapper.selectCustomerListAdmin(query);
			list.stream().forEach(c -> {
				c.setIdNo(CommonUtils.idNoFormat(c.getIdNo(), 4, 4));
			});
		}
		return new PageBean<>(list, total);
	}

	@Override
	public CustomerDetailAdminVO selectCustomerDetailAdmin(Long customerId) {
		
		CustomerDetailAdminVO resultVo = customerMapper.selectCustomerDetailAdmin(customerId);
		state(resultVo != null, "客户详情信息异常");
		
		//查询客户联系人信息
		List<CustomerContactInfoVO> contactInfoList = customerContactMapper.selectCustomerContactInfoByCustomerId(customerId);
		contactInfoList.stream().forEach(c -> {
			c.setRelationshipText(c.getRelationship().getText());
		});
		
		resultVo.setContactInfoList(contactInfoList);
		
		return resultVo;
	}

	@Override
	@Transactional
	public void doEdit(CustomerAdminEditAO editAO) {
		
		Date time =  new Date();
		Long customerId = editAO.getCustomerId();
		
		//更新客户信息
		Customer updateCustomer = new Customer();
		BeanUtils.copyProperties(editAO, updateCustomer);
		updateCustomer.setId(customerId);
		updateCustomer.setUpdateTime(time);
		customerMapper.updateByPrimaryKeySelective(updateCustomer);
		
		//更新客户联系人信息
		List<CustomerContact> customerContactList = editAO.getCustomerContactList();
		if(!CommonUtils.isEmpty(customerContactList)) {
			for(CustomerContact customerContact : customerContactList) {
				customerContact.setCustomerId(customerId);
				customerContact.setCreateTime(time);
				customerContact.setUpdateTime(time);
			}
			//批量新增
			customerContactMapper.insertBatch(customerContactList);
			//删除无效数据
			customerContactMapper.deleteByUpdateTime(customerId, time);
		}
		
	}

	@Override
	public MallCustomerDetailAdminVO selectMallCustomerDetailAdminVO(Long customerId) {
		//后台-查询马先生商城客户详情
		MallCustomerDetailAdminVO modelVo = customerMapper.selectMallCustomerDetailAdminVO(customerId);
		state(modelVo != null, "客户不存在");
		
		//查询客户联系人信息
		List<CustomerContactInfoVO> contactInfoList = customerContactMapper.selectCustomerContactInfoByCustomerId(modelVo.getCustomerId());
		contactInfoList.stream().forEach(c -> {
			c.setRelationshipText(c.getRelationship().getText());
		});
		
		//查询客户房产证明
		List<CustomerImageListVO> housePropertyProofList = customerImageMapper.selectCustomerImageListByCustomerId(customerId, CustomerImageType.HOUSE_PROPERTY_PROOF);
		if(!CommonUtils.isEmpty(housePropertyProofList)) {
			housePropertyProofList.stream().forEach(i -> {
				i.setSrc(fileService.getFileUrl(i.getPath(), Constants.MALL_BUCKET_NAME));
			});
		}
		
		//查询客户结婚证明
		List<CustomerImageListVO> marriageProofList = customerImageMapper.selectCustomerImageListByCustomerId(customerId, CustomerImageType.MARRIAGE_PROOF);
		if(!CommonUtils.isEmpty(marriageProofList)) {
			marriageProofList.stream().forEach(i -> {
				i.setSrc(fileService.getFileUrl(i.getPath(), Constants.MALL_BUCKET_NAME));
			});
		}
		
//		Map<String, Object> map = JsonObjectMapper.read(modelVo.getBigDataCheckResponseJson(), Map.class);
//		modelVo.setBigDataCheckResponseMap(map);
		modelVo.setBigDataCheckResponseJson(null);
		modelVo.setSexText(modelVo.getSex().getText());
		modelVo.setAge(CommonUtils.getAgeByIdNo(modelVo.getIdNo()));
		modelVo.setBigDataCheckResultText(modelVo.getBigDataCheckResult().getText());
		modelVo.setFaceidCheckResultText(modelVo.getFaceidCheckResult().getText());
		modelVo.setContactInfoList(contactInfoList);
		modelVo.setHousePropertyProofList(housePropertyProofList);
		modelVo.setMarriageProofList(marriageProofList);
		
		return modelVo;
	}

	
}
